//
//  UIView+RedBadge.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/12/27.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//

#import "UIView+RedBadge.h"
#define TabbarItemNums 4.0    //tabbar的数量 如果是5个设置为5.0

@implementation UIView (RedBadge)

/**
 tabbar显示小红点
 
 */

- (void)showRedBadgeCount:(int)count{
    
    [self removeBadgeView];
    
    if (count == 0) {
        return;
    }
    //label为小红点，并设置label属性
    UILabel *label = [[UILabel alloc]init];
    label.tag = 1000;
    label.clipsToBounds = YES;
    label.layer.cornerRadius = __I6Width(6);
    
    
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.font = KSystemFont(__I6Width(8));
    label.backgroundColor = KCSREDTEXTCOLOR;
    CGRect tabFrame = self.frame;
    
    
    //计算小红点的X值，根据第index控制器，小红点在每个tabbar按钮的中部偏移0.1，即是每个按钮宽度的0.6倍
    CGFloat percentX = 0.6;
    CGFloat tabBarButtonW = CGRectGetWidth(self.frame);
    CGFloat x = percentX*tabBarButtonW - 2;
    CGFloat y = 0.1*CGRectGetHeight(tabFrame);
    //10为小红点的高度和宽度
    if (count < 999) {
        label.frame = CGRectMake(x, y, __I6Width( 12)+ count * 0.1, __I6Width(12));
        label.text = [NSString stringWithFormat:@"%d",count];
        
    }else{
        label.frame = CGRectMake(x, y,__I6Width(20), __I6Width(12));
        
        label.text = [NSString stringWithFormat:@"%d+",999];
        
    }
    
    
    [self addSubview:label];
    //把小红点移到最顶层
    [self bringSubviewToFront:label];
    
    
    
}
/**
 隐藏红点
 
 @param index 第几个控制器隐藏，从0开始算起
 */
-(void)hideBadgeOnItemIndex:(int)index{
    [self removeBadgeView];
}

/**
 移除控件
 
 @param index 第几个控制器要移除控件，从0开始算起
 */
- (void)removeBadgeView{
    for (UIView*subView in self.subviews) {
        
        [subView removeFromSuperview];

    }
}

@end
